<?php

class House extends Module
{
    function __construct()
    {
    }    
    private static $_obj = null;
    static function getInstance()
    {
        if( !self::$_obj )
        {
            self::$_obj = new self();
        }

        return self::$_obj;
    }
    
    function get()
    {     
        $ret = array('ret'=>true, 'info'=>'succes');
        $data = array( 'a' );
        $id = isset( $_POST['id'] ) ? trim( $_POST['id'] ) : '';   
        
        
		$url = 'http://bj.58.com/haidian/hezu/?final=1&searchtype=3&key=%2525u4E2D%2525u5173%2525u6751%252520%2525u53F7%2525u697C&sourcetype=5';
		$doc = phpQuery::newDocumentFile( $url );
		$str = $doc['table tr']->html();
		phpQuery::each($doc['table tr'], 'func', new CallbackParam, new CallbackParam);
		global $g_data;
		//*
		//$content = file_get_contents( $url );
		//$lines = explode( "\n", $content );
		//$lines = explode( "\n", $str );
		$data = array_merge( $g_data, $data );
		
		HosTools::getInstance()->storeInfo( $g_data );
		
        $ret['data'] = $data;
        echo json_encode( $ret );
		//*/
    }

    function display()
    {
        $id = isset( $_GET['id'] ) ? trim( $_GET['id'] ) : '';
        $ret = array( 'ret'=>true, 'data'=>$id );
        parent::smartyOut('house', $ret);
    }
}

function func($key, $value)
{
	global $g_data;
	$tmp = array();
	// j_2_40048814_13237068474626_3_2
	// http://bj.58.com/hezu/13237068474626x.shtml
	$tmp['id'] = pq($value)->attr("logr");
	$tmp['url'] = pq($value)->find("a.t")->attr("href");
	$tmp['img'] = pq($value)->find("img")->attr("src");
	$tmp['info'] = pq($value)->find(".t")->text();
	$tmp['price'] = pq($value)->find(".tc:eq(0)")->text();
	$tmp['time'] = pq($value)->find(".tc:eq(1)")->text();
	foreach( $tmp as &$one )
	{
		$one = trim( str_replace( "\n", "", $one ) );
	}
	if( $tmp['img'] && $tmp['info'] )
	{
		$idinfo = explode( "_", $tmp['id'] );
		$tmp['id'] = $idinfo[3];
		$g_data[] = $tmp;
	}
}























